package com.cskaoyan.wordmemorize.dao.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.cskaoyan.wordmemorize.dao.entity.UserVocBoundStatisticsDO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;

public interface UserVocBoundStatisticsMapper extends BaseMapper<UserVocBoundStatisticsDO> {

    // 统计用户免费获取的总单词上限
    Integer sumFreeVocBound(@Param("userId") Long userId);

    // 统计用户购买的总单词上限
    Integer sumPayVocBound(@Param("userId") Long userId);

    // 统计用户总单词上限
    Integer totalVocBound(@Param("userId") Long userId);



    @Update("UPDATE user_voc_bound_statistics " +
            "SET available = available - #{deduct}, " +
            "occupied = occupied + #{deduct}, " +
            "updated_time = NOW() " +
            "WHERE user_id = #{userId} AND available >= #{deduct}")
    int deductAvailable(@Param("userId") Long userId,
                        @Param("deduct") Integer deduct);  // 减少可用单词上限

}
